package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
  @Autowired
    private SpuDetailService spuDetailService;

  /**
   * 
   * @param page
   * @param rows
   * @param saleable 
   * @return org.springframework.http.ResponseEntity<com.leyou.common.dto.PageDTO<SpuDTO>>
   * @creed: Talk is cheap,show me the code
   * @date 2020/8/26 12:17
   */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>>pageQuery(
           @RequestParam(value = "page",defaultValue = "1") Integer page,
           @RequestParam(value = "rows",defaultValue = "5") Integer rows,
           @RequestParam(value = "saleable",required = false) Boolean saleable,
           @RequestParam(value = "categoryId",required = false) Long categoryId,
           @RequestParam(value = "brandId",required = false) Long brandId,
           @RequestParam(value = "id",required = false) Long id

           ){
        return ResponseEntity.ok(this.spuService.pageQuery(page,rows,saleable,categoryId,brandId,id));
    }

    /**
     * 商品上下架(修改或者新增等没有返回值的时，泛型为Void)
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void>modifySaleable(
           @RequestParam("id") Long id,
           @RequestParam("saleable") Boolean saleable){
this.spuService.modifySaleable(id,saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 商品新增业务
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
  public ResponseEntity<Void>addGoods(@RequestBody SpuDTO spuDTO){

   this.spuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据spuId查询对应的商品所有信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO>queryGoodsBySpuId(@PathVariable("id") Long id){

         return ResponseEntity.ok(this.spuService.queryGoodsBySpuId(id)) ;
    }

    @PutMapping("/spu")
    public ResponseEntity<Void>updateGoods(@RequestBody SpuDTO spuDTO){
        this.spuService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spuId查询spu的基本信息
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO>querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new SpuDTO(this.spuService.getById(id)));
    }

    /**
     * 根据sku的id集合查询对应的sku集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>>querySkuByIds(@RequestParam("ids") List<Long>ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(this.skuService.listByIds(ids)));
    }
    /**
     * 根据spuId查询对应的sku信息
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>>querySkuBySpuId(@RequestParam("id")Long spuId){
        return ResponseEntity.ok(this.skuService.querySkuBySpuId(spuId));
    }

    /**
     * 根据spuId查询对应的spuDetail的信息
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail")
   public ResponseEntity<SpuDetailDTO>querySpuDetailById(@RequestParam("id") Long spuId){
        return ResponseEntity.ok(new SpuDetailDTO(this.spuDetailService.getById(spuId)));
    }

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>>querySpecValue(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching",required = false) Boolean searching){
        return ResponseEntity.ok(this.spuDetailService.querySpecValue(id,searching));
    }
    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }


    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long,Integer> cartsMap){

        this.skuService.plusStock(cartsMap);
        return ResponseEntity.ok().build();
    }
}
